smahal <- function(z,X,cv){
  
  X <- as.matrix(X)
  n <- dim(X)[1]
  rownames(X) <- 1:n
  k <- dim(X)[2]
  m<-sum(z)
  
  for(j in 1:k) X[,j] <- rank(X[,j])
  
  vuntied <- var(1:n)
  rat <- sqrt(vuntied/diag(cv))
  cv <- diag(rat) %*% cv %*%diag(rat)
  out <- matrix(NA,m,n-m)
  
  Xc <- X[z==0,]
  Xt <- X[z==1,]
  
  rownames(out) <- rownames(X)[z==1]
  colnames(out) <- rownames(X)[z==0]
  
  icov <- ginv(cv)
  
  for(i in 1:m){
    out[i,] <- mahalanobis(Xc,Xt[i,],icov,inverted=T)
  }
  
  return(out)
  
}